package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.api.domain.Region;
import cn.wolfcode.wolf2w.business.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.business.query.DestinationQuery;
import cn.wolfcode.wolf2w.business.service.IDestinationService;
import cn.wolfcode.wolf2w.business.service.IRegionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 目的地Service业务层处理
 * 
 * @author fang
 * @date 2024-09-27
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper,Destination> implements IDestinationService {
    @Autowired
    private IRegionService regionService;

    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Destination> search(Long regionId) {

        //在查子集
        List<Destination> list;
        if (regionId == -1){
            //国内
            list = lambdaQuery().eq(Destination::getParentId, 1).list();

        }else{
            //其他
            Region region = regionService.getById(regionId);
            String refIds = region.getRefIds();
            //查询出数组后进行封装
            String[] split = refIds.split(",");
            List<Long> ids  = new ArrayList<>();
            for (String s : split) {
                //将分别拆封的数字转化成long类型
                Long id = Long.valueOf(s);
                ids.add(id);
            }
            list = baseMapper.selectBatchIds(ids);
        }

        //查询二级目的地
        for (Destination destination : list) {
            Long id = destination.getId();
            List<Destination> children = lambdaQuery().eq(Destination::getParentId, id)
                    .last("limit 10")
                    .list();
            destination.setChildren(children);
        }

        return list;
    }


    @Override
    public List<Destination> toasts(Long destId) {
        List<Destination> list = new ArrayList<>();
        createToasts(destId,list);
//        Collections.reverse(list);
        return list;
    }

    //递归函数，两个参数，第一个是目的地id，第二个是最终生成的目的地数组
    //如，参数一是355，参数二是一个数组：["中国","广东","广州"]
    private void createToasts(Long destId,List<Destination> list){
        //递归结束条件
        if (destId == null) {
            return;
        }
        Destination destination = baseMapper.selectById(destId);

        //上一级id
        Long parentId = destination.getParentId();
        //递归使用，再次调用
        createToasts(parentId,list);
        list.add(destination);

    }
}
